package com.mmy.controller.permissionmenu;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.permissionmenu.TConstMenu;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.permissionmenu.impl.MenuServiceImpl;

/**
 * 类的介绍：菜单信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-06
 * @version 1.0
 */
@Controller
public class MenuController {

	@Autowired
	MenuServiceImpl menuServiceImpl;

	/**
	 * 函数功能说明： 跳转前台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */
	@RequestMapping(value = "/frontmenu")
	public String toFrontMenu(HttpServletRequest request, String pageNumber, String pageSize, Model model) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");
		String strChildMenu = request.getParameter("strChildMenu");

		List<TConstMenu> lstConstMenu = menuServiceImpl.frontMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		HashMap<String, Integer> mapPage = menuServiceImpl.mapFrontMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		model.addAttribute("lstConstMenu", lstConstMenu);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strType", strType);
		model.addAttribute("strChildMenu", strChildMenu);
		model.addAttribute("strMenuName", strMenuName);

		return "webbackground/permission-menu/FrontMenu";

	}

	/**
	 * 函数功能说明： 跳转后台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */

	@RequestMapping("/backmenu")
	public String toBackMenu(HttpServletRequest request, String pageNumber, String pageSize, Model model) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");
		String strChildMenu = request.getParameter("strChildMenu");

		List<TConstMenu> lstConstMenu = menuServiceImpl.backMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		HashMap<String, Integer> mapPage = menuServiceImpl.mapBackMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		model.addAttribute("lstConstMenu", lstConstMenu);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strType", strType);
		model.addAttribute("strChildMenu", strChildMenu);
		model.addAttribute("strMenuName", strMenuName);
		return "webbackground/permission-menu/BackMenu";
	}

	/**
	 * 函数功能说明： 跳转新建菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */
	@RequestMapping(value = "/createmenu")
	public String toCreateMenu(Model model) {

		List<TConstMenu> lstConstMenu = menuServiceImpl.findAll();
		model.addAttribute("lstConstMenu", lstConstMenu);
		return "webbackground/permission-menu/CreateMenu";
	}

	/**
	 * 函数功能说明： 新建菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改
	 * @date 创建日期 2020-07-13
	 */

	@RequestMapping(value = "/savecreatemenu")
	public String saveCreateMenu(TConstMenu tConstMenu, HttpSession session) throws Exception {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		menuServiceImpl.createMenu(tConstMenu, tSysUser);

		return "redirect:frontmenu";
	}

	/**
	 * 函数功能说明： 删除菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */

	@PostMapping("/delmenu")
	@ResponseBody
	public String delMenu(String checkList) {

		menuServiceImpl.delMenu(checkList);

		return "webbackground/permission-menu/Menu";
	}

	/**
	 * 函数功能说明： 跳转修改菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */

	@RequestMapping(value = "/toupdatemenu")
	public String toUpdateMenu(HttpServletRequest request, Model model) {

		int iMenuID = Integer.parseInt(request.getParameter("iMenuID"));

		TConstMenu tConstMenu = menuServiceImpl.findByMenuID(iMenuID);
		model.addAttribute("tConstMenu", tConstMenu);

		int iUpMenuID = tConstMenu.getUpMenuID();

		if (iUpMenuID == 0) {

			model.addAttribute("strUpName", "顶级菜单");
		} else {
			String strUpName = menuServiceImpl.findByMenuID(iUpMenuID).getMenuName();
			model.addAttribute("strUpName", strUpName);
		}

		return "webbackground/permission-menu/UpdateMenu";

	}

	/**
	 * 函数功能说明： 修改菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-13
	 */
	@RequestMapping(value = "/updatemenu")
	public String updateMenu(TConstMenu tConstMenu, HttpSession session) throws Exception {

		try {
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			menuServiceImpl.updateMenu(tConstMenu, tSysUser);
			return "redirect:frontmenu";

		} catch (Exception e) {

		}
		return null;
	}

//	TODO delete
//	/**
//	 * 函数功能说明： 查询前台菜单
//	 *
//	 * @param 参数说明
//	 * @return 返回值说明 jsp页面
//	 * @throws 异常处理说明
//	 * @author 创建人 shangzl
//	 * @date 创建日期 2020-07-13
//	 */
//	@RequestMapping(value = "/queryfrontmenu")
//	public String queryFrontMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
//			String pageSize) {
//
//		String strMenuName = request.getParameter("strMenuName");
//		String strType = request.getParameter("strType");
//
//		HashMap<String, Integer> mapPage = menuServiceImpl.mapFrontMenu(pageNumber, pageSize, strMenuName, strType,
//				strChildMenu);
//
//		List<TConstMenu> lstConstMenu = menuServiceImpl.queryFrontMenu(strMenuName, strType, strChildMenu, pageNumber,
//				pageSize, mapPage);
//
//		model.addAttribute("lstConstMenu", lstConstMenu);
//		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
//		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
//		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
//		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
//		model.addAttribute("strType", strType);
//		model.addAttribute("strChildMenu", strChildMenu);
//		model.addAttribute("strMenuName", strMenuName);
//		return "webbackground/permission-menu/FrontMenu";
//	}

//	TODO delete
//	/**
//	 * 函数功能说明： 查询后台菜单
//	 *
//	 * @param 参数说明
//	 * @return 返回值说明 jsp页面
//	 * @throws 异常处理说明
//	 * @author 创建人 shangzl
//	 * @date 创建日期 2020-07-13
//	 */
//	
//	@RequestMapping(value = "/querybackmenu")
//	public String queryBackMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
//			String pageSize) {
//
//		String strMenuName = request.getParameter("strMenuName");
//		String strType = request.getParameter("strType");
//
//		HashMap<String, Integer> mapPage = menuServiceImpl.mapBackMenu(pageNumber, pageSize, strMenuName, strType,
//				strChildMenu);
//		List<TConstMenu> lstConstMenu = menuServiceImpl.queryBackMenu(strMenuName, strType, strChildMenu, pageNumber,
//				pageSize, mapPage);
//
//		model.addAttribute("lstConstMenu", lstConstMenu);
//		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
//		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
//		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
//		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
//		model.addAttribute("strType", strType);
//		model.addAttribute("strChildMenu", strChildMenu);
//		model.addAttribute("strMenuName", strMenuName);
//		return "webbackground/permission-menu/BackMenu";
//	}

	/**
	 * 函数功能说明： 上移前台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/upfrontmenu")
	public String upFrontMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
			String pageSize) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");

		int iID = Integer.valueOf(request.getParameter("iID"));

		HashMap<String, Integer> mapPage = menuServiceImpl.mapFrontMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		List<TConstMenu> lstConstMenu = menuServiceImpl.upFrontMenu(iID, strMenuName, strType, strChildMenu, pageNumber,
				pageSize, mapPage);

		model.addAttribute("lstConstMenu", lstConstMenu);
		return "redirect:frontmenu";

	}

	/**
	 * 函数功能说明： 下移前台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/downfrontmenu")
	public String downFrontMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
			String pageSize) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");
		int iID = Integer.valueOf(request.getParameter("iID"));
		HashMap<String, Integer> mapPage = menuServiceImpl.mapFrontMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		List<TConstMenu> lstConstMenu = menuServiceImpl.downFrontMenu(iID, strMenuName, strType, strChildMenu,
				pageNumber, pageSize, mapPage);
		model.addAttribute("lstConstMenu", lstConstMenu);
		return "redirect:frontmenu";
	}

	/**
	 * 函数功能说明： 上移后台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/upbackmenu")
	public String upBackMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
			String pageSize) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");
		int iID = Integer.valueOf(request.getParameter("iID"));
		HashMap<String, Integer> mapPage = menuServiceImpl.mapBackMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		List<TConstMenu> lstConstMenu = menuServiceImpl.upBackMenu(iID, strMenuName, strType, strChildMenu, pageNumber,
				pageSize, mapPage);
		model.addAttribute("lstConstMenu", lstConstMenu);
		return "redirect:backmenu";
	}

	/**
	 * 函数功能说明： 下移后台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/downbackmenu")
	public String downBackMenu(HttpServletRequest request, Model model, String strChildMenu, String pageNumber,
			String pageSize) {

		String strMenuName = request.getParameter("strMenuName");
		String strType = request.getParameter("strType");
		int iID = Integer.valueOf(request.getParameter("iID"));
		HashMap<String, Integer> mapPage = menuServiceImpl.mapBackMenu(pageNumber, pageSize, strMenuName, strType,
				strChildMenu);

		List<TConstMenu> lstConstMenu = menuServiceImpl.downBackMenu(iID, strMenuName, strType, strChildMenu,
				pageNumber, pageSize, mapPage);
		model.addAttribute("lstConstMenu", lstConstMenu);
		return "redirect:backmenu";
	}

}
